﻿#region Librerias
using System;
using System.Collections.Generic;
using System.Web;
using System.Data;
#endregion

/// <summary>
/// Class User, define los datos de un usuario del sistema
/// </summary>

#region Class

public class clsUser : DAC
{
    #region Variables

    private int idUser, idPNatural, idRole;
    private String sName, sPass;
    private const String tabla = "dbo.tblUser";

    #endregion

    #region Builder

    /// <summary>
    /// Constructor
    /// </summary>
    public clsUser() { }

    #endregion

    #region Methods

    #region Consultations

    public DataSet SeeAllTheRecords()
    {
        String query = "SELECT row_number() over(order by " + tabla + ".idUser) As NUMBER, ";
        query += tabla + ".idUser As ID, " + tabla + ".idPNatural As NATURAL, ";
        query += tabla + ".idRole As ROLE, " + tabla + ".sName As NAME, " + tabla + ".sPass As PASS ";
        query += "FROM " + tabla;
        return this.ExecuteQuery(query);
    }

    public DataSet SeeAllTheRecords(int startRecord, int maxRecord, String text)
    {
        String query = "SELECT row_number() over(order by " + tabla + ".idUser) As NUMBER, ";
        query += tabla + ".idUser As ID, " + tabla + ".idPNatural As NATURAL, ";
        query += tabla + ".idRole As ROLE, " + tabla + ".sName As NAME, " + tabla + ".sPass As PASS ";
        query += "FROM " + tabla;
        if (!text.Equals(""))
        {
            query += " WHERE " + tabla + ".sName LIKE '%" + text + "%' ";
        }
        if (startRecord == 0 && maxRecord == 0)
        {
            return this.ExecuteQuery(query);
        }
        else
        {
            return this.ExecuteQuery(query, startRecord, maxRecord);
        }
    }

    public DataSet ConsultID(String id)
    {
        String query = "SELECT ";
        query += tabla + ".idUser As ID, " + tabla + ".idPNatural As NATURAL, ";
        query += tabla + ".idRole As ROLE, " + tabla + ".sName As NAME, " + tabla + ".sPass As PASS ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".idUser=" + id;
        return this.ExecuteQuery(query);
    }

    public DataSet ConsultRoleID(String id)
    {
        String query = "SELECT ";
        query += tabla + ".idUser As ID, " + tabla + ".idPNatural As NATURAL, ";
        query += tabla + ".idRole As ROLE, " + tabla + ".sName As NAME, " + tabla + ".sPass As PASS ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".idRole=" + id;
        return this.ExecuteQuery(query);
    }

    public DataSet ConsultPNaturalID(String id)
    {
        String query = "SELECT ";
        query += tabla + ".idUser As ID, " + tabla + ".idPNatural As NATURAL, ";
        query += tabla + ".idRole As ROLE, " + tabla + ".sName As NAME, " + tabla + ".sPass As PASS ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".idPNatural=" + id;
        return this.ExecuteQuery(query);
    }

    public DataSet UserConsult(String username, String password)
    {
        String query = "SELECT ";
        query += tabla + ".idUser As ID, " + tabla + ".idPNatural As NATURAL, ";
        query += tabla + ".idRole As ROLE, " + tabla + ".sName As NAME, " + tabla + ".sPass As PASS ";
        query += "FROM " + tabla;
        query += " WHERE " + tabla + ".sName='" + username + "' ";
        query += "AND " + tabla + ".sPass='" + password + "' ";
        return this.ExecuteQuery(query);
    }

    #endregion

    public int Insert()
    {
        String query = "INSERT INTO " + tabla;
        query += "(idPNatural, idRole, sName, sPass) ";
        query += "VALUES ('{0}','{1}','{2}','{3}')";

        String sql = String.Format(query, this.IdPNatural, this.IdRole, this.SName, this.SPass);
        return this.ExecuteDML(sql, "no");
    }

    public int Update()
    {
        String query = "UPDATE " + tabla + " SET ";
        query += tabla + ".idPNatural=" + this.IdPNatural + ", " + tabla + ".idRole=" + this.IdRole + ", ";
        query += tabla + ".sName='" + this.SName + "', " + tabla + ".sPass='" + this.SPass + "' ";
        query += "WHERE ";
        query += tabla + ".idUser=" + this.ID;
        return this.ExecuteDML(query, "no");
    }

    public int Delete()
    {
        String query = "DELETE FROM " + tabla + " WHERE " + tabla + ".idUser={0}";
        String sql = String.Format(query, this.ID);
        return this.ExecuteDML(sql, "no");
    }

    #endregion

    #region Setters and Getters

    public int IdRole
    {
        get { return idRole; }
        set { idRole = value; }
    }

    public int IdPNatural
    {
        get { return idPNatural; }
        set { idPNatural = value; }
    }

    public int ID
    {
        get { return idUser; }
        set { idUser = value; }
    }

    public String SPass
    {
        get { return sPass; }
        set { sPass = value; }
    }

    public String SName
    {
        get { return sName; }
        set { sName = value; }
    }

    #endregion

}

#endregion